/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.autogenerats;

import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Abonat;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.ModeAplicacio;
import entity.Produccio;
import entity.Abono;

/**
 *
 * @author David
 */
public class AbonatJpaController implements Serializable {

    public AbonatJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Abonat abonat) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ModeAplicacio modeAplicacioId = abonat.getModeAplicacioId();
            if (modeAplicacioId != null) {
                modeAplicacioId = em.getReference(modeAplicacioId.getClass(), modeAplicacioId.getId());
                abonat.setModeAplicacioId(modeAplicacioId);
            }
            Produccio produccioId = abonat.getProduccioId();
            if (produccioId != null) {
                produccioId = em.getReference(produccioId.getClass(), produccioId.getId());
                abonat.setProduccioId(produccioId);
            }
            Abono abonoId = abonat.getAbonoId();
            if (abonoId != null) {
                abonoId = em.getReference(abonoId.getClass(), abonoId.getId());
                abonat.setAbonoId(abonoId);
            }
            em.persist(abonat);
            if (modeAplicacioId != null) {
                modeAplicacioId.getAbonatCollection().add(abonat);
                modeAplicacioId = em.merge(modeAplicacioId);
            }
            if (produccioId != null) {
                produccioId.getAbonatCollection().add(abonat);
                produccioId = em.merge(produccioId);
            }
            if (abonoId != null) {
                abonoId.getAbonatCollection().add(abonat);
                abonoId = em.merge(abonoId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Abonat abonat) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Abonat persistentAbonat = em.find(Abonat.class, abonat.getId());
            ModeAplicacio modeAplicacioIdOld = persistentAbonat.getModeAplicacioId();
            ModeAplicacio modeAplicacioIdNew = abonat.getModeAplicacioId();
            Produccio produccioIdOld = persistentAbonat.getProduccioId();
            Produccio produccioIdNew = abonat.getProduccioId();
            Abono abonoIdOld = persistentAbonat.getAbonoId();
            Abono abonoIdNew = abonat.getAbonoId();
            if (modeAplicacioIdNew != null) {
                modeAplicacioIdNew = em.getReference(modeAplicacioIdNew.getClass(), modeAplicacioIdNew.getId());
                abonat.setModeAplicacioId(modeAplicacioIdNew);
            }
            if (produccioIdNew != null) {
                produccioIdNew = em.getReference(produccioIdNew.getClass(), produccioIdNew.getId());
                abonat.setProduccioId(produccioIdNew);
            }
            if (abonoIdNew != null) {
                abonoIdNew = em.getReference(abonoIdNew.getClass(), abonoIdNew.getId());
                abonat.setAbonoId(abonoIdNew);
            }
            abonat = em.merge(abonat);
            if (modeAplicacioIdOld != null && !modeAplicacioIdOld.equals(modeAplicacioIdNew)) {
                modeAplicacioIdOld.getAbonatCollection().remove(abonat);
                modeAplicacioIdOld = em.merge(modeAplicacioIdOld);
            }
            if (modeAplicacioIdNew != null && !modeAplicacioIdNew.equals(modeAplicacioIdOld)) {
                modeAplicacioIdNew.getAbonatCollection().add(abonat);
                modeAplicacioIdNew = em.merge(modeAplicacioIdNew);
            }
            if (produccioIdOld != null && !produccioIdOld.equals(produccioIdNew)) {
                produccioIdOld.getAbonatCollection().remove(abonat);
                produccioIdOld = em.merge(produccioIdOld);
            }
            if (produccioIdNew != null && !produccioIdNew.equals(produccioIdOld)) {
                produccioIdNew.getAbonatCollection().add(abonat);
                produccioIdNew = em.merge(produccioIdNew);
            }
            if (abonoIdOld != null && !abonoIdOld.equals(abonoIdNew)) {
                abonoIdOld.getAbonatCollection().remove(abonat);
                abonoIdOld = em.merge(abonoIdOld);
            }
            if (abonoIdNew != null && !abonoIdNew.equals(abonoIdOld)) {
                abonoIdNew.getAbonatCollection().add(abonat);
                abonoIdNew = em.merge(abonoIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = abonat.getId();
                if (findAbonat(id) == null) {
                    throw new NonexistentEntityException("The abonat with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Abonat abonat;
            try {
                abonat = em.getReference(Abonat.class, id);
                abonat.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The abonat with id " + id + " no longer exists.", enfe);
            }
            ModeAplicacio modeAplicacioId = abonat.getModeAplicacioId();
            if (modeAplicacioId != null) {
                modeAplicacioId.getAbonatCollection().remove(abonat);
                modeAplicacioId = em.merge(modeAplicacioId);
            }
            Produccio produccioId = abonat.getProduccioId();
            if (produccioId != null) {
                produccioId.getAbonatCollection().remove(abonat);
                produccioId = em.merge(produccioId);
            }
            Abono abonoId = abonat.getAbonoId();
            if (abonoId != null) {
                abonoId.getAbonatCollection().remove(abonat);
                abonoId = em.merge(abonoId);
            }
            em.remove(abonat);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Abonat> findAbonatEntities() {
        return findAbonatEntities(true, -1, -1);
    }

    public List<Abonat> findAbonatEntities(int maxResults, int firstResult) {
        return findAbonatEntities(false, maxResults, firstResult);
    }

    private List<Abonat> findAbonatEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Abonat.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Abonat findAbonat(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Abonat.class, id);
        } finally {
            em.close();
        }
    }

    public int getAbonatCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Abonat> rt = cq.from(Abonat.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
